package com.eveinfo.display;

import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;

import com.eveinfo.EveInfo;
import com.eveinfo.R;
import com.eveinfo.api.accessors.Certificates;
import com.eveinfo.api.utils.certificates.ApiCategory;
import com.eveinfo.display.certificates.AllCertificatesFilter;
import com.eveinfo.display.certificates.CategoriesAdapter;
import com.eveinfo.display.certificates.CertificatesFilter;
import com.eveinfo.tools.Logging;

public class CategoriesView extends Activity {

	public static Certificates certificates;

	private Activity activity;
	private Spinner spinner;

	private enum Items {
		all("All certificates", new AllCertificatesFilter());

		private final String name;
		private final CertificatesFilter filter;

		private Items(String name, CertificatesFilter filter) {
			this.name = name;
			this.filter = filter;
		}

		public CertificatesFilter getFilter() {
			return filter;
		}

		@Override
		public String toString() {
			return name;
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.skills);
		activity = this;

		final int characterID = getIntent().getIntExtra("characterID", 0);

		try {
			certificates = EveInfo.getData().getCertificates();
			if (certificates == null) {
				return;
			}
			spinner = (Spinner) findViewById(R.id.SSkills);
			ArrayAdapter<Items> adapter = new ArrayAdapter<Items>(activity, android.R.layout.simple_spinner_item, Items
					.values());

			adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
			spinner.setAdapter(adapter);
			init(characterID);
		} catch (Exception e) {
			Logging.getInstance().e(e);
			e.printStackTrace();
			AlertDialog.Builder builder = new AlertDialog.Builder(this);
			builder.setTitle(e.toString());
			builder.setMessage(e.getMessage());
			builder.show();
		}
	}

	public void init(int characterID) {
		List<ApiCategory> categories = certificates.getCertificates().getCategories();
		ListView listview = (ListView) findViewById(R.id.LVSkills);
		final CategoriesAdapter certificatesAdapter = new CategoriesAdapter(activity, listview, categories,
				new AllCertificatesFilter(), characterID);
		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Items i = (Items) spinner.getSelectedItem();
				certificatesAdapter.setFilter(i.getFilter());
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});
		certificatesAdapter.notifyDataSetChanged();
	}

}
